<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link  rel="stylesheet"  type="text/css" href="/stylesheets/reg.css">
    <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
    </script>

</head>
<body>
    <div class="contrainer">
        <div class="top">
            <p>注册</p>
        </div>

        <div class="middle">
            <input id="username" type="text" placeholder="用户名" onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;">
            <input id="newpwd" type="password" placeholder="新密码" onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;">
            <input id="conpwd" type="password" placeholder="确认密码" onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;">
        </div>

        <div class="bottom">
            <button onclick="backClick()">返回</button>

            <button onclick="reClick()">注册</button>
        </div>

    </div>

    <script>
        function backClick() {
            window.location.href='login';

        }
        
        function reClick() {
            var username = document.getElementById("username").value;
            var newpwd = document.getElementById("newpwd").value;
            var conpwd = document.getElementById("conpwd").value;

            if (username==""){
                alert("用户名不能为空");
            }
            else if(newpwd==""){
                alert("新密码不能为空");
            }
            else if(conpwd==""){
                alert("确认密码不能为空");
            }
            else if(conpwd!=newpwd){
                alert("两次密码不一致");
            }
            else {
                $.post("/reg",{
                        username:username,
                        password:newpwd
                    },
                    function(data,status){
                        var dataObj=eval("("+data+")");//转换为json对象
                        if(dataObj.status==200||dataObj.status==201){
                            window.location.href='home';
                        }
                        else {
                            alert("数据: \n" + dataObj.msg + "\n状态: " + dataObj.status);
                        }

                    });
            }


        }
        
    </script>

</body>
</html>